package com.cx.core.magazine.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.cx.core.magazine.dao.ArticleDao;
import com.cx.core.magazine.model.bean.Article;
import com.li.core.daoBase.DAOBase;
import com.mysql.jdbc.Statement;

public class ArticleDaoImpl extends DAOBase implements ArticleDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cx.core.magazine.dao.ArticleDao#queryAriticlesByPeriodId(int)
	 */
	@Override
	public List<Article> queryAriticlesByPeriodId(int periodId) {
		List<Article> articles = new ArrayList<Article>();
		Integer[] params = { periodId };
		String sql = "select * from Article where period_id = ? order by type,publish_date desc";
		List list = this.getJdbcT().queryForList(sql, params);
		Map m = null;
		Article article = null;
		for (Object o : list) {
			m = (Map) o;
			article = new Article();
			article.setId((Integer)m.get("id"));//id
			article.setPeriod_id((Integer)m.get("period_id"));//期号
			article.setTitle((String)m.get("title"));//标题
			article.setType((String)m.get("type"));//类别
			article.setContent((String)m.get("content"));//内容
			article.setPublisher((String)m.get("publisher"));//发布者
			article.setPublish_date(((Timestamp)m.get("publish_date")).getTime());//操作时间
			article.setImage_id(String.valueOf(m.get("image_id")));//图像信息
			//article.setUser((String)m.get("user"));
			//article.setTimestamp(((Date)m.get("timestamp")).getTime());		
			articles.add(article);
		}
		return articles;
	}

	@Override
	public int saveArticle(Article article) {
		Object[] pars = new Object[]{ article.getPeriod_id(),article.getTitle(),article.getType(),article.getContent(),article.getImage_id(),article.getPublisher(),new Timestamp(article.getPublish_date()),article.getUser() };
		final Article art = article;
		final String sql = "insert into article(period_id,title,type,content,image_id,publisher,publish_date,userid,timestamp)  VALUES(?,?,?,?,?,?,?,?,now())";
		KeyHolder kh = new GeneratedKeyHolder();
		this.getJdbcT().update(new PreparedStatementCreator(){

			@Override
			public PreparedStatement createPreparedStatement(Connection conn)
					throws SQLException {
				PreparedStatement ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				ps.setInt(1, art.getPeriod_id());
				ps.setString(2, art.getTitle());
				ps.setString(3, art.getType());
				ps.setString(4, art.getContent());
				ps.setString(5, art.getImage_id());
				ps.setString(6, art.getPublisher());
				ps.setTimestamp(7, new Timestamp(art.getPublish_date()));
				ps.setString(8, art.getUser());
				return ps;	
			}
			
		},kh);
		article.setId(kh.getKey().intValue());
		return 0;
	}

	@Override
	public int saveArticles(List<Article> articles) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void updateArticle(Article article) {
		Object[] pars = new Object[]{ article.getTitle(),article.getType(),article.getContent(),article.getImage_id(),article.getPublisher(),new Timestamp(article.getPublish_date()),article.getUser(),article.getId() };
		String sql = "update article set title=?,type=?,content=?,image_id=?,publisher=?,publish_date=?,userid=?,timestamp=now() where id=? ";
		this.getJdbcT().update(sql,pars);
	}

	@Override
	public void deleteArticle(int id) {
		// TODO Auto-generated method stub
		Object[] pars = new Object[]{ id };
		String sql = "delete from article where id=?";
		this.getJdbcT().update(sql,pars);
	}

}
